﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using wms_wpf.Apiservice;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;

namespace wms_wpf.ViewModels
{
    public class HouseConfigViewModel:BindableBase, INavigationAware
    {
        public HouseConfigViewModel(HouseConfigView view)
        {
            GetList_WarehouseLineCommand = new DelegateCommand(GetList_WarehouseLine);
            GetListViewInlethouseLineCommand=new DelegateCommand(GetListViewInlethouseLine);
            PageUpdatedCmd = new DelegateCommand(GetList_WarehouseLine);
            EnableCommand = new DelegateCommand(Enable);
            DisableCommand = new DelegateCommand(Disable);
            IndexCommand = new DelegateCommand<string>(Index);
            service = new BaseService();
            BaseDto2 = new BaseDto2();
            WarehouseLineDtos = new List<WarehouseLineDto>();
            View_InlethouseLineDtos = new List<View_InlethouseLineDto>();
            InPut_BaseService = new InPut_BaseService4();
            InPut_ViewService3=new InPut_ViewService3();
            viewService =new ViewService();
            this.service = service;
            InPut_BaseService.PageIndex = 1;
            InPut_BaseService.PageSize = 20;
            InPut_ViewService3.PageIndex = 1;
            InPut_ViewService3.PageSize = 20;
            this.View = view;
            GetList_WarehouseLine();
            GetListViewInlethouseLine();
            GetWarehousequantity();


        }
        public DelegateCommand<string> IndexCommand { get; set; }
        public DelegateCommand GetList_WarehouseLineCommand { get; set; }
        public DelegateCommand GetListViewInlethouseLineCommand { get; set; }
        public DelegateCommand PageUpdatedCmd { get; set; }
        public DelegateCommand EnableCommand { get; set; }
        public DelegateCommand DisableCommand { get; set; }

        private HouseConfigView View { get; set; }
        private IBaseService service { get; set; }
        private IViewService viewService { get; set; }
        private BaseDto2 baseDto2;
        public BaseDto2 BaseDto2
        {
            get { return baseDto2; }
            set { baseDto2 = value; RaisePropertyChanged(); }
        }

        private List<WarehouseLineDto> warehouseLineDtos;
        public List<WarehouseLineDto> WarehouseLineDtos
        {
            get { return warehouseLineDtos; }
            set { warehouseLineDtos = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService4 inPut_BaseService;
        public InPut_BaseService4 InPut_BaseService
        {
            get { return inPut_BaseService; }
            set { inPut_BaseService = value;RaisePropertyChanged(); }
        }
        private InPut_ViewService3 inPut_ViewService3;
        public InPut_ViewService3 InPut_ViewService3
        {
            get { return inPut_ViewService3; }
            set { inPut_ViewService3 = value; RaisePropertyChanged(); }
        }
        private List<View_InlethouseLineDto> view_InlethouseLineDtos;
        public List<View_InlethouseLineDto> View_InlethouseLineDtos
        {
            get { return view_InlethouseLineDtos; }
            set { view_InlethouseLineDtos = value; RaisePropertyChanged(); }
        }

        private long count;
        public long Count
        {
            get { return count; }
            set { count = value;RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value;RaisePropertyChanged(); }
        }

        private long count2;
        public long Count2
        {
            get { return count2; }
            set { count2 = value; RaisePropertyChanged(); }
        }
        private long pagecount2;
        public long PageCount2
        {
            get { return pagecount2; }
            set { pagecount2 = value; RaisePropertyChanged(); }
        }


        private int selectIndex;
        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }


        private void Index(string obj)
        {
            switch (obj)
            {
                case "TwoPage": SelectIndex = 1;  break;
                case "OnePage": SelectIndex = 0;  break;

            }
        }
        private async void Enable()//启用
        {
            try
            {
                InPut_BaseService5 inPut = new InPut_BaseService5();
                if (View.datagrid.SelectedItems.Count == 0)
                {
                    return;
                }
                foreach (WarehouseLineDto item in View.datagrid.SelectedItems)
                {
                    inPut.WarehouseLineCodes.Add(item.WarehouseLineCode);

                }
                inPut.Type = true;
                bool res = await service.Update_WarehouseLines(inPut);
                if (res)
                {
                    GetList_WarehouseLine();
                }
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-Enable", ex);
            }
       
        }
        private async void Disable()//禁用
        {
            try
            {
                InPut_BaseService5 inPut = new InPut_BaseService5();
                if (View.datagrid.SelectedItems.Count == 0)
                {
                    return;
                }
                foreach (WarehouseLineDto item in View.datagrid.SelectedItems)
                {
                    inPut.WarehouseLineCodes.Add(item.WarehouseLineCode);
                }
                inPut.Type = false;
                bool res = await service.Update_WarehouseLines(inPut);
                if (res)
                {
                    GetList_WarehouseLine();
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-Disable", ex);
            }
    
        }  
        private async void GetList_WarehouseLine()//获取数据
        {

            try
            {
                Tuple<List<WarehouseLineDto>, long?, long?> tuple = await service.GetList_WarehouseLine(InPut_BaseService);
                if (tuple == null)
                {
                    return;
                }
                WarehouseLineDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;
            }  catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetList_WarehouseLine", ex);
            }     
        
       
        }
        private async void GetListViewInlethouseLine()//获取入仓口
        {

            try
            {
                Tuple<List<View_InlethouseLineDto>, long?, long?> tuple = await viewService.GetList_View_InlethouseLine(InPut_ViewService3);
                if (tuple == null)
                {
                    return;
                }
                View_InlethouseLineDtos = tuple.Item1;
                Count2 = (long)tuple.Item2;
                PageCount2 = (long)tuple.Item3;
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetListViewInlethouseLine", ex);
            }
     
        }
        private async void GetWarehousequantity()
        {
            try
            {
                BaseDto2 = await service.GetWarehousequantity();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetWarehousequantity", ex);
            }
         
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            GetListViewInlethouseLine();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }
    }
}
